/*
* creat by jun
* */
grApp.controller('MemberCtrl',['$scope','$http',function($scope,$http){

    $scope.bindorchange=function(args){
        //alert(args);
        if(args=="bind"){
            $scope.showbdsj={display:'block'};
            $scope.showczsj={display:'none'};
            $scope.words='请输入要绑定的手机号码';
        }else if(args.length>0){
            $scope.showbdsj={display:'none'};
            $scope.showczsj={display:'block'};
            $scope.words='更换绑定的手机号码';

            var qiansan=args.substring(0,3);
            var housi=args.substring(7);
            var bdp=qiansan+'****'+housi;
            $scope.lbdph=bdp;
            $scope.truebdphone=args;
            $scope.isver=false;//用于判断用户是否已经验证原手机号
        }

    };
    //判断用户是否绑定手机
    $scope.ifBingPhone=function(args){
        $http({
            methd:'GET',
            url:'/user/ifbindphone'
        }).success(function(data,status,headers,config){
            if(!data.bindphone) {
                //没有绑定手机，弹出模态框，进行绑定
                var value='';
                if('c'==args){
                    value = prompt("亲～绑定一下手机号，可以吗？");
                    //alert(value);
                }else if('m'==args){
                    value = prompt("请输入您要绑定的手机号：");
                }
                var re = /^1\d{10}$/;
                if(value !==null) {
                    if (re.test(value)) {
                        $scope.bindPhone(value);
                    } else {
                        alert('手机号码格式不对哦！');
                    }
                }
            }else{
                if('m'==args) {
                    alert('亲，您已经绑定过手机号啦！');
                }
            }
        }).error(function(data,status,headers,config){
            alert('网络连接异常！');
        });
    };
    //绑定手机
    $scope.bindPhone=function(args){
        $http({
            methd:'GET',
            url:'/user/bindphone/'+args
        }).success(function(data, status, headers, config){
            alert('手机绑定成功，谢谢！');
        }).error(function(data, status, headers, config){
            alert('网络连接异常！');
        });
    };

    $scope.getsmscode=function(args){
        var smsphone=$('#smsphone').val();
        if(smsphone){
            var re = /^1\d{10}$/;
            if(re.test(smsphone)){
                $scope.smsphone=smsphone;
                $http({
                    methd:'GET',
                    url:'/user/smsphone/'+smsphone
                }).success(function(data, status, headers, config){
                    //alert(data);
                    if(data.sendsms){
                        alert('验证码发送成功');
                    }else{
                        alert('验证码发送失败');
                    }
                }).error(function(data, status, headers, config){
                    alert('验证码发送失败');
                });
            }else{
                alert('亲，手机号格式不对哦');
            }
        }else{
            alert('亲，手机号不能是空的哦');
        }
    };

    $scope.verifySmsCode=function(args){
        var smscode=$('#smscode').val();
        //alert(smscode);
        if (!smscode) {
            alert('验证码不能为空');
        } else {
            $http({
                methd: 'GET',
                url: '/user/smscode',
                params: {lcode:smscode,lphone:$scope.smsphone}
            }).success(function (data, status, headers, config) {
                if (data.ifsmscode) {
                    alert('手机绑定成功');
                    window.location.href='/user/member';
                } else {
                    alert('验证码错误');
                }
            }).error(function (data, status, headers, config) {
                alert('验证码错误');
            });
        }
    };

    $scope.jarpwd=function(args){
        if(args=="sz"){
            $scope.pwdtitle='设置余额支付密码';
            $scope.showsz={display:'block'};
            $scope.showxg={display:'none'};
            $scope.showzh={display:'none'};
        }else if(args=="xg"){
            $scope.pwdtitle='修改余额支付密码';
            $scope.showsz={display:'none'};
            $scope.showxg={display:'block'};
            $scope.showzh={display:'none'};
        }else if(args=="zh"){
            $scope.pwdtitle='找回余额支付密码';
            $scope.showsz={display:'none'};
            $scope.showxg={display:'none'};
            $scope.showzh={display:'block'};
        }
    };

    //用户更换手机号，向原绑定的手机发送验证码
    $scope.getcode=function(args){
        var smsphone=$scope.truebdphone;
        if(smsphone){
            var re = /^1\d{10}$/;
            if(re.test(smsphone)){
                $scope.smsphone=smsphone;
                $http({
                    methd:'GET',
                    url:'/user/smsphone/'+smsphone
                }).success(function(data, status, headers, config){
                    //alert(data);
                    if(data.sendsms){
                        alert('验证码发送成功');
                    }else{
                        alert('验证码发送失败');
                    }
                }).error(function(data, status, headers, config){
                    alert('验证码发送失败');
                });
            }else{
                alert('亲，手机号格式不对哦');
            }
        }else{
            alert('亲，手机号不能是空的哦');
        }
    };

    $scope.qrcode=function(args){
        var smscode=$('#ghpcode').val();
        var oldphone=$scope.truebdphone;
        if(!smscode){
            alert('验证码不能为空');
        }else{
            $http({
                methd:'GET',
                url:'/user/ghbdphone',
                params:{scode:smscode,oldphone:oldphone}
            }).success(function(data, status, headers, config){
                if(data.ifsmscode){
                    $('#newphone').attr('placeholder','验证码');
                    $scope.isver=true;
                    alert('原手机号码解锁成功');
                }else(
                    alert('验证码错误，解锁失败')
                )
            }).error(function(data, status, headers, config){
                alert('验证码错误，解锁失败')
            })
        }
    };

    //获取绑定新手机号码的验证码
    $scope.huoqucode=function(args){
        var newphone=$('#newphone').val();
        if(!$scope.isver){
            alert('请先解锁原手机号码')
        }else if($scope.isver){
            if(newphone){
                var re = /^1\d{10}$/;
                if(re.test(newphone)){
                    $scope.nphone=newphone;
                    $http({
                        methd:'GET',
                        url:'/user/smsphone/'+newphone
                    }).success(function(data, status, headers, config){
                        //alert(data);
                        if(data.sendsms){
                            alert('验证码发送成功');
                        }else{
                            alert('验证码发送失败');
                        }
                    }).error(function(data, status, headers, config){
                        alert('验证码发送失败');
                    });
                }else{
                    alert('亲，手机号格式不对哦');
                }
            }else{
                alert('亲，手机号不能是空的哦');
            }
        }
    };

    //绑定新手机号码
    $scope.ghsavephone=function(args){
        var newphone=$scope.nphone;
        var smscode=$('#ghyzcode').val();

        if (!smscode) {
            alert('验证码不能为空');
        } else {
            $http({
                methd: 'GET',
                url: '/user/smscode',
                params: {lcode:smscode,lphone:newphone}
            }).success(function (data, status, headers, config) {
                if (data.ifsmscode) {
                    alert('手机绑定成功');
                    window.location.href='/user/member';
                } else {
                    alert('验证码错误');
                }
            }).error(function (data, status, headers, config) {
                alert('验证码错误');
            });
        }
    };

    $scope.savepwd=function(args){
        var pwd=$('#yepwd').val();
        if(!pwd){
            alert('密码不能为空');
        }else if(pwd.length!=6){
            alert('支付密码长度必需为6位')
        }else if(pwd.length==6) {
            var repwd = $('#qryepwd').val();
            if(pwd==repwd){
                $http({
                    methd:'GET',
                    url:'/user/saveyepwd',
                    params:{yepwd:pwd}
                }).success(function(data, status, headers, config){
                    if(data.savebool){
                        alert('余额支付密码保存成功')
                    }else{
                        alert('余额支付密码保存失败')
                    }
                }).error(function(data, status, headers, config){
                    alert('余额支付密码保存失败')
                })
            }else if(pwd!=repwd){
                alert('确认密码错误');
            }
        }
    };

    //修改支付密码
    $scope.changePassword=function(args){

        var ypwd=$('#yyepwd').val();
        var xpwd=$('#xyepwd').val();
        var qrxpwd=$('#qrxyepwd').val();
        if(!xpwd||!ypwd||!qrxpwd){
            alert('密码不能为空');
        }else if(xpwd.length!=6){
            alert('支付密码长度必须为6位')
        }else if(xpwd!=qrxpwd){
            alert('确认密码错误')
        }else if(xpwd.length==6){
            $http({
                methd:'GET',
                url:'/user/xgpassword',
                params:{ypwd:ypwd,qrxpwd:qrxpwd}
            }).success(function(data, status, headers, config){
                if(data.ppwdres=='pwd'){
                    alert('支付密码修改成功');
                    window.location.href='/user/member';
                }else if(data.ppwdres=='cwpwd'){
                    alert('密码修改失败1')
                }else if(data.ppwdres=='ypwd'){
                    alert('原密码错误')
                }
            }).error(function(data, status, headers, config){
                alert('密码修改失败2')
            })
        }
    };

    //忘记密码 找回，发送验证码到绑定手机
    $scope.sendpwdcode=function(args){
        $http({
            methd:'GET',
            url:'/user/findpwd'
        }).success(function(data, status, headers, config){
            if(data.send=='sendcode'){
                alert('验证码发送成功');
            }else if(data.send=='bdpw'){
                alert('您尚未绑定手机号');
            }else if (data.send=='failed'){
                alert('验证码发送失败');
            }

        }).error(function(data, status, headers, config){
            alert('验证码发送失败');
        })
    };

    //获取验证码，判断密码，保存密码
    $scope.savecodepwd=function(args) {
        var code = $('#smscode').val();
        var zhpwd = $('#zhpwd').val();
        var qrzhpwd = $('#qrzhpwd').val();
        if(!code){
            alert('验证码不能为空');
        }else if(!zhpwd||!qrzhpwd){
            alert('支付密码不能为空')
        }else if(zhpwd!=qrzhpwd){
            alert('确认密码与密码不一致')
        }else{
            $http({
                methd:'GET',
                url:'/user/savewjpwd',
                params:{code:code,zhpwd:zhpwd}
            }).success(function(data, status, headers, config){
                if(data.setpwd){
                    alert('支付密码设置成功')
                    window.location.href='/user/member';
                }else{
                    alert('验证码错误，支付密码设置失败')
                }
            }).error(function(data, status, headers, config){
                alert('支付密码设置失败')
            })
        }

    }

}]);
